package gwtI18nEditor.datamodel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Message {
	private final MessageFile parent; 
	private String            identifier;
	private MessageType		  type;
	private final String      prefix;
	private final String      infix;
	private final String      suffix;
	private List<Translation> translations;
	private boolean           complete = true;
	
	public Message(MessageFile parent, String identifier, MessageType type, String prefix, String infix, String suffix) {
		this.parent       = parent;
		this.identifier   = identifier;
		this.type         = type;
		this.prefix       = prefix;
		this.infix        = infix;
		this.suffix       = suffix;
		this.translations = new ArrayList<Translation>();
	}
	
	public Message(MessageFile parent, String identifier) {
		this.parent       = parent;
		this.identifier   = identifier;
		this.type         = MessageType.UNKNOWN;
		this.prefix       = "\n\t\t";
		this.infix        = " ";
		this.suffix       = "();";
		this.translations = new ArrayList<Translation>();
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public MessageType getType() {
		return type;
	}

	public void setType(MessageType type) {
		this.type = type;
	}

	public String getPrefix() {
		return prefix;
	}

	public String getInfix() {
		return infix;
	}

	public String getSuffix() {
		return suffix;
	}

	public List<Translation> getTranslations() {
		return translations;
	}
	
	public void addTranslation(Locale language, String message) {
		translations.add(new Translation(this, language, message));
	}
	
	public boolean needsAttention() {
		return !complete;
	}
	
	@Override
	public String toString() {
		return identifier;
	}
	
	public void check(Locale... locales) {
		// Check off existing translations from locale requirements list
		List<Locale> left = new ArrayList<Locale>(Arrays.asList(locales));
		List<Translation> removals = new ArrayList<Translation>();
		for (Translation translation: translations)
			if (!translation.needsAttention()) {
				left.remove(translation.getLocale());
			} else {
				if (!left.contains(translation))
					removals.add(translation);
			}
		// Remove all blank translation place-holders that are not in the required locales list
		for (Translation translation: removals)
			translations.remove(translation);
		// Add blank translation place-holders for all currently non-existent, but required locales  
		for (Locale locale: left)
			addTranslation(locale, null);
		// Update complete status and notify parent of change
		if (left.isEmpty()!=complete) {
			complete = left.isEmpty();
			if (complete) parent.removeIncomplete();
			         else parent.   addIncomplete();
		}
	}

	public void reCheck() {
		boolean done = true;
		for (Translation translation: translations)
			if (translation.needsAttention()) done = false;
		if (done!=complete) {
			complete = done;
			if (complete) parent.removeIncomplete();
			         else parent.   addIncomplete();
		}
	}
}
